package com.atguigu.edu.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.edu.entity.EduSubject;
import com.atguigu.edu.entity.ExcelSubject;
import com.atguigu.edu.mapper.EduSubjectMapper;
import com.atguigu.edu.service.EduSubjectService;
import com.atguigu.exception.EduException;
import com.atguigu.listener.MySubjectListener;
import com.atguigu.response.SubjectVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;


import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author HB
 * @since 2020-11-06
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {


    /**
     * 2. 查询所有课程的分类
     * @return
     */
    @Override
    public List<SubjectVO> getAllSubject() {
        //没有查询条件的去查找所有eduSubject
        List<EduSubject> subjectList = baseMapper.selectList(null);
        //将所有的一级分类放在 parentSubjectVos集合中
        List<SubjectVO> parentSubjectVos = new ArrayList<>();
        for (EduSubject eduSubject : subjectList) {
            //1. 先查询所有的一级分类 判断标准	parentId=0
            if (eduSubject.getParentId().equals("0")){
                SubjectVO subjectVO = new SubjectVO();
                //数据库中的字段【eduSubject】里面的属性包含subjectVO
                //将eduSubject的属性赋值给subjectVO存在的属性
                BeanUtils.copyProperties(eduSubject,subjectVO);
                //将一级分类放到parentSubjectVos集合中
                parentSubjectVos.add(subjectVO);
            }
        }
        //2.把一级分类放到一个角落(map)
        Map<String, SubjectVO> parentSubjectMap = new HashMap<>();
        for (SubjectVO parentSubjectVo : parentSubjectVos) {
            parentSubjectMap.put(parentSubjectVo.getId(),parentSubjectVo);
        }
        //3. 查询所有的二级分类
        for (EduSubject eduSubject : subjectList) {
            //4. 先查询所有的2级分类 判断标准	parentId!=0
            if (!eduSubject.getParentId().equals("0")){
                //5. 拿到二级分类的parentId(组号) 从map中找一级分类
                SubjectVO parentSubjectVo = parentSubjectMap.get(eduSubject.getParentId());
                //6. 一级分类里面有个children属性 将二级节点放到children属性下面 成为他的自己点
                SubjectVO childSubjectVo = new SubjectVO();
                //为二级节点赋值
                BeanUtils.copyProperties(eduSubject,childSubjectVo);
                //7. 将二级节点放到一级节点下
                List<SubjectVO> children = parentSubjectVo.getChildren();
                children.add(childSubjectVo);
            }
        }
        return parentSubjectVos;
    }

    /**
     * 1. 导入课程分类信息到数据库
     *
     * @param file
     */
    @Autowired
    private MySubjectListener mySubjectListener;
    @Override
    public void uploadSubject(MultipartFile file) throws Exception {

        //1. 获取到文件的流数据
        InputStream inputStream = file.getInputStream();
        //2. 需要自定义一个监听器
        EasyExcel.read(inputStream, ExcelSubject.class, mySubjectListener).doReadAll();
    }


    /**
     * 查看数据库中是否已经存在一级分类:
     * 判断条件是 标题与parentId不同
     *
     * @param parentSubjectName title
     * @param parentId
     */
    @Override
    public EduSubject existSubject(String parentSubjectName, String parentId) {
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("title", parentSubjectName);
        wrapper.eq("parent_id", parentId);
        EduSubject eduSubject = baseMapper.selectOne(wrapper);
        return eduSubject;
    }

    /**
     * 根据id删除节点
     * @param id
     * @return
     */
    @Override
    public boolean deleteSubjectById(String id) {
        //先查看它是不是一级节点
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        //比较传输过来的节点是不是一级节点
        wrapper.eq("parent_id",id);
        Integer integer = baseMapper.selectCount(wrapper);
        if (integer == 0) {
            int rows = baseMapper.deleteById(id);
            return rows>0;
        }else {
            throw new EduException(20001,"该节点存在子节点");
        }
    }

    /**
     * 添加一级分类
     * @param eduSubject
     * @return
     */
    @Override
    public boolean saveParentSubject(EduSubject eduSubject) {
        EduSubject parentSubject = existSubject(eduSubject.getTitle(), "0");
        int rows=0;
        if (parentSubject == null) {
            parentSubject=new EduSubject();
            parentSubject.setTitle(eduSubject.getTitle());
            parentSubject.setParentId("0");
            rows = baseMapper.insert(parentSubject);
        }
        return rows>0;

    }

    /**
     * 添加二级分类
     * @param eduSubject
     * @return
     */
    @Override
    public boolean saveChildSubject(EduSubject eduSubject) {
        EduSubject childSubject = existSubject(eduSubject.getTitle(), eduSubject.getParentId());
        int rows=0;
        if (childSubject == null) {
            rows = baseMapper.insert(eduSubject);
        }
        return rows>0;
    }
}
